//
//  PEDeviceStatusCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/12/5.
//

#import "RHEnergyStorageStatusCell.h"
#import "RHBatteryView.h"
#import "RHTopoModel.h"

@interface RHEnergyStorageStatusCell()
@property (nonatomic, strong) RHBatteryView *batteryView;

@property (nonatomic, strong) QMUILabel *statusLab;

@property (nonatomic, strong) QMUILabel *socLab;

@property (nonatomic, strong) QMUILabel *timeTitleLab;

@property (nonatomic, strong) QMUILabel *timeLab;


@end
@implementation RHEnergyStorageStatusCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    return self;
}


-(void)setTopoModel:(RHTopoModel *)topoModel
{
    _topoModel = topoModel;
    //0.静置 1.工作 2.离线
    if ([topoModel.status integerValue] == 0) {//在线
        if ([topoModel.batPwr floatValue] == 0) {
            self.statusLab.text = @"静置".rh_localized;
        }else{
            self.statusLab.text = @"工作".rh_localized;
        }
    }
    
    
    NSDecimalNumber *socValue = [NSDecimalNumber decimalNumberWithDouble:topoModel.soc];
    self.socLab.text = [NSString stringWithFormat:@"%@%%",socValue];
    self.batteryView.soc = topoModel.soc /100.0f;
    
    NSInteger timeInterval = 0;
    
    if ([topoModel.batPwr floatValue]>0) {
        timeInterval = [topoModel.chgTim integerValue];
        self.timeTitleLab.text = [NSString stringWithFormat:@"%@:",@"备电时长".rh_localized];
    }
    if ([topoModel.batPwr floatValue]<0) {
        timeInterval = [topoModel.dischgTim integerValue];
        self.timeTitleLab.text = [NSString stringWithFormat:@"%@:",@"剩余放电时长".rh_localized];
    }
    NSInteger hour = timeInterval / 60 ;
    NSInteger minute = timeInterval % 60;
    self.timeLab.text = [NSString stringWithFormat:@"%ld %@ %ld %@",hour,@"小时".rh_localized,minute,@"分".rh_localized];
    if(timeInterval == 0){
        self.timeLab.text = @"";
        self.timeTitleLab.text = @"";
    }
    
    if ([topoModel.status integerValue] == 1) {//离线
        self.statusLab.text = @"离线".rh_localized;
        self.timeLab.text = @"";
        self.timeTitleLab.text = @"";
    }
}

- (void)setupUI{
    kWeakSelf;
    [self setBackgroundColor:CFTabViewBgColor];
    [self.contentView setBackgroundColor:CFTabViewBgColor];
    
    UIView *bgView = [[UIView alloc] init];
    [bgView rounded:8 width:1 color:kBorderColor];
    [bgView setBackgroundColor:UIColor.whiteColor];
    [self.contentView addSubview:bgView];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0,15, 0, 15));
    }];
    
    UIView *leftView;
//    if([self.reuseIdentifier isEqualToString:@"PEDeviceStatusCell_233"]){
//        UIImageView *devIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"device_233_icon"]];
//        [bgView addSubview:devIcon];
//        leftView = devIcon;
//        [devIcon mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.width.height.offset(50);
//            make.left.mas_offset(10);
//            make.centerY.mas_offset(0);
//        }];
//    }
    
    _batteryView = [[RHBatteryView alloc] init];
    [bgView addSubview:_batteryView];
    [_batteryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(33, 18)));
        make.top.mas_offset(15);
        if(leftView){
            make.left.equalTo(leftView.mas_right).offset(5);
        }else{
            make.left.mas_offset(15);
        }
    }];
    
    QMUILabel *statusTitleLab = [[QMUILabel alloc] init];
    statusTitleLab.text = [NSString stringWithFormat:@"%@:",@"状态".rh_localized];
    [statusTitleLab setTextAlignment:NSTextAlignmentLeft];
    [statusTitleLab setFont:[UIFont systemFontOfSize:14]];
    [statusTitleLab setTextColor:kCOLOR_HEX(@"3F4443")];
    [bgView addSubview:statusTitleLab];
    [statusTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.batteryView.mas_right).offset(5);
        make.centerY.equalTo(weakSelf.batteryView);
    }];
    
    _statusLab = [[QMUILabel alloc] init];
    _statusLab.text = @"--".rh_localized;
    [_statusLab setTextAlignment:NSTextAlignmentLeft];
    [_statusLab setFont:[UIFont systemFontOfSize:14]];
    [_statusLab setTextColor:kCOLOR_HEX(@"3F4443")];
    [bgView addSubview:_statusLab];
    [_statusLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(statusTitleLab.mas_right);
        make.centerY.equalTo(statusTitleLab);
    }];
    
    _socLab = [[QMUILabel alloc] init];
    _socLab.text = @"--";
    [_socLab setTextAlignment:NSTextAlignmentRight];
    [_socLab setFont:kSYSTEMFONT(12)];
    [_socLab setTextColor:[UIColor colorWithHexString:@"3F4443" alpha:0.6]];
    [bgView addSubview:_socLab];
    [_socLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.batteryView);
        make.top.equalTo(weakSelf.batteryView.mas_bottom).offset(8);
        make.bottom.mas_offset(-15);
    }];
    
    _timeTitleLab = [[QMUILabel alloc] init];
    _timeTitleLab.text = [NSString stringWithFormat:@"%@:",@"备电时长".rh_localized];
    [_timeTitleLab setTextAlignment:NSTextAlignmentLeft];
    [_timeTitleLab setFont:kSYSTEMFONT(12)];
    [_timeTitleLab setTextColor:[UIColor colorWithHexString:@"3F4443" alpha:0.6]];
    [bgView addSubview:_timeTitleLab];
    
    [_timeTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.socLab.mas_right).offset(3);
        make.centerY.equalTo(weakSelf.socLab);
    }];
    
    _timeLab = [[QMUILabel alloc] init];
    _timeLab.text = @"--";
    [_timeLab setTextAlignment:NSTextAlignmentRight];
    [_timeLab setFont:kSYSTEMFONT(12)];
    [_timeLab setTextColor:kCOLOR_HEX(@"79E75A")];
    [bgView addSubview:_timeLab];
    [_timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.timeTitleLab.mas_right).offset(5);
        make.centerY.equalTo(weakSelf.timeTitleLab);
    }];
    
    UIImageView *arrowImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"kwt_home_arrow"]];
    [bgView addSubview:arrowImg];
    [arrowImg   mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(15, 15)));
        make.centerY.mas_offset(0);
        make.right.mas_offset(-15);
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];
    [bgView addGestureRecognizer:tap];
    [tap.rac_gestureSignal subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
        if(weakSelf.detailBlock){
            weakSelf.detailBlock();
        }
    }];
}

-(void)dealloc{
    
}

@end
